import json
import datetime
import os
from collections import Counter
import csv
import time
import datetime
# ht=[ '#YearOnTikTok', '#lovestory','#artmas','#festivefashion','#happyhanukkah','#planttiktok','#productivity','#ImAGhost','#catchphrases','#ootd','#comfortfood','#holidaycrafts','#watchmegrow','#holidaydecor','#raisedby','#makeitvogue','#goodmorning','#watchingxfactor','#catchphrases','#rnbvibes','#gamenight','#HorrorToK', '#ProblemSolved', '#SkateLife', '#ThriftShop', '#SpookyTreats',  '#tiktokfood', '#interiordesign', '#sfxmakeup', '#ComingOfAge', '#falldiy', '#worldseries', '#gaminglife', '#meleaving', '#yellow', '#yougotthis','#mycostume','#mypfp','#welldone','#nativefamily','#happyhalloween','#homeoffice','#myrecommendation','#myhobby','#fanedit','#stemlife','#motivationmonday','#wip','#veteransday','#holidaycountdown','#WeekendVibes','#holidaysourway','#RoomTour','#bakingszn','#nbadraft',
#      '#nonuancenovember','#gamingsetup','#carsoftiktok','#needtoknow','#whenwewereyounger','#halloweenishere','#ourtype','#fallguysmoments','#workingathome','#onlinedating','#familyrecipe','#givingthanks','#diceroll','#holidayvibes','#holidaytiktok','#recordsday','#inkdrawing','#happyholidays','#homecooked','#neonshadow',  '#youwantmore' ,'#familyimpression','#givingszn','#whereilive','foodtiktok','#holidaymusic','#howbizarre', '#personalfinance'
#      ,'#thinkingabout', '#housetour','#readysetshop','#easydiy','#coldweather','#wildanimals','#festivefashion','graphicdesign','smallbusiness','#halloweenlook','fallfashion','#artmas','#haventseen','#planttiktok','#productivity','#bekind','#onhold','#growupwithme','#tabletop','#theatrekids','#wishlist','#haventseen','#selfimprovement', '#OhNo','#animation']
ht=['#HolidaysOurWay']
ht=[h.lower().replace('#','') for h in ht]


for hashtag in ht:
    datalist=[]
    dict={}
    for name in os.listdir('D:\\Work\\Tool\\tiktok\\TikToks\\'):
        if hashtag in name.lower():
            datalist.append(name)
    with open('D:\\Work\\Tool\\tiktok\\ctvl_'+hashtag+'.tsv', 'w', encoding='utf-8',
              newline='\n') as filename_output:
        writer = csv.writer(filename_output, delimiter='\t')
        row=['id','create time','video length']
        print(len(row))
        writer.writerow(row)
        print(row)
        for i in range(len(datalist)):
            ranking=0
            with open('D:\\Work\\Tool\\tiktok\\TikToks\\' + datalist[i], 'r', encoding='utf-8', newline='\n') as filename_input:
                f=[]
                for line in filename_input:
                    z = json.loads(line)
                    if 'id' in z.keys():
                        id=z['id']
                        ct=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(z['createTime'])))
                        vl=z['video']['duration']
                    elif 'itemInfos' in z.keys():
                        id=z['itemInfos']['id']
                        ct=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(z['itemInfos']['createTime'])))
                        vl=z['itemInfos']['video']['videoMeta']['duration']
                    if id in dict.keys():
                        continue
                    else:
                        dict[id]=[ct,vl]
        j=0
        for key in dict.keys():
            rows=[key,dict[key][0],dict[key][1]]
            if j<100:
                print(rows)
                j+=1
            writer.writerow(rows)


    # dict[hashtag] = [datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S'),
    #                  datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S'), datalist[0].split('_')[2]]
    #
    # print(hashtag,dict[hashtag][0],dict[hashtag][1],dict[hashtag][2])
    # writer.writerow([hashtag,dict[hashtag][0],dict[hashtag][1],dict[hashtag][2]])